var leave=[]
var tbodys=document.querySelector("tbody")
var pageSize = 7; //每页显示的记录条数
var page = 1; //当前页，默认第一页


// 查询请假列表
function seleqing(){
    var sels=document.querySelector("#sel")
    var index=localStorage.getItem("userid")
    var num=Number(index)
    var rle=localStorage.getItem("userRole")
    if(rle==="管理员"){
        rle=Number(1)
    }else if(rle==="超级管理员"){
        rle=Number(0)
    }else if(rle==="老师"){
        rle=Number(2)
    }else if(rle==="学生"){
        rle=Number(3)
    }
    let sales={
        userId:num,
        userRole:rle,
        pageindex:page,
        pagenum:pageSize,
        name:sels.value
    }
    ajaxHttp("post","/relax/findRelaxList",sales).then((res)=>{
        res=JSON.parse(res)
        // console.log(res);
        var data=res.dataset.relaxList
        leave=data
        for(var i=0;i<leave.length;i++){
            var newtr=document.createElement("tr")
            newtr.innerHTML= `
                 <td>${leave[i].userId}</td>
                                <td>${leave[i].relaxPri}</td>
                                <td>${leave[i].relaxReason}</td>
                                <td>${leave[i].createTime}</td>
                                <td>${leave[i].relaxDate}</td>
                                <td>${leave[i].relaxDuration}</td>
                                <td class="stat">${leave[i].status}</td>
            `
            tbodys.appendChild(newtr)
            var stat=document.querySelectorAll(".stat")
           for(var j=0;j<stat.length;j++){
            if(stat[j].innerText=="未通过"){
                stat[j].style.color="red"
            }else if(stat[j].innerText=="通过"){
                stat[j].style.color="green"
            }
           }
           if(sels.value===leave[i].userId){
            tbodys.innerHTML=""
                for(var z=0;z<leave.length;z++){
                    var newtr=document.createElement("tr")
                    newtr.innerHTML= `
                         <td>${leave[i].userId}</td>
                                        <td>${leave[i].relaxPri}</td>
                                        <td>${leave[i].relaxReason}</td>
                                        <td>${leave[i].createTime}</td>
                                        <td>${leave[i].relaxDate}</td>
                                        <td>${leave[i].relaxDuration}</td>
                                        <td class="stat">${leave[i].status}</td>
                    `
                    tbodys.appendChild(newtr)
                }
           }
        }
        
      
        
    })
}
seleqing()

var addopens=document.querySelector(".addopen")

//单击弹出添加出差记录弹窗
function addopen(){
    addopens.style.display="flex"
}

// 添加请假记录接口
function addqing(){
    var names=localStorage.getItem("userName")
    var index=localStorage.getItem("userid")
    var namea=document.querySelector(".namea")
    namea.value=names
   var bs=document.querySelector("#bs");
   var shi=document.querySelector(".shi")
   var setdate=document.querySelector(".setdate")
   var date=document.querySelector(".date")
    let qingj={
        relaxDate:setdate.value,
        relaxDuration:date.value,
        relaxReason:shi.value,
        userId:Number(index),
        relaxPri:Number(bs.value),
        name:namea.value
    }
    ajaxHttp("post","relax/addRelax",qingj).then((res)=>{
        res=JSON.parse(res)
        alert("添加成功"),
        offadd()
        location.reload()
    })
    
}

function offadd(){
    addopens.style.display="none"
}


// 分页
var theUL = document.getElementById("pageListTableBody");
var totalPage = document.getElementById("spanTotalPage");
var pageNum = document.getElementById("spanPageNum"); //获取当前页<span>
var spanPre = document.getElementById("spanPre"); //获取上一页<span>
var spanNext = document.getElementById("spanNext"); //获取下一页<span>
var spanFirst = document.getElementById("spanFirst"); //获取第一页<span>
var spanLast = document.getElementById("spanLast"); //获取最后一页<span>
var numberRowsInTable = theUL.getElementsByTagName("tr").length; //记录总条数


//下一页
function next(){
    hideTable();
    currentRow = pageSize * page;
    maxRow = currentRow + pageSize;
    if ( maxRow > numberRowsInTable ) maxRow = numberRowsInTable;
    for ( var i = currentRow; i< maxRow; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    page++;
    if ( maxRow == numberRowsInTable ) {
        nextText();
        lastText();
    }
    showPage();
    preLink();
    firstLink();
}

//上一页
function pre(){
    hideTable();
    page--;
    currentRow = pageSize * page;
    maxRow = currentRow - pageSize;
    if ( currentRow > numberRowsInTable ) currentRow = numberRowsInTable;
    for ( var i = maxRow; i< currentRow; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    if ( maxRow == 0 ){
        preText();
        firstText();
    }
    showPage();
    nextLink();
    lastLink();
}

//第一页
function first(){
    hideTable();
    page = 1;
    for ( var i = 0; i<pageSize; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    firstText();
    preText();
    nextLink();
    lastLink();
}

//最后一页
function last(){
    hideTable();
    page = pageCount();
    currentRow = pageSize * (page - 1);
    for ( var i = currentRow; i<numberRowsInTable; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    preLink();
    nextText();
    firstLink();
    lastText();
}

function hideTable(){
    for ( var i = 0; i<numberRowsInTable; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
}

function showPage(){
    pageNum.innerHTML = page;
}

//总共页数
function pageCount(){
    return Math.ceil(numberRowsInTable/pageSize);
}
//显示链接
function preLink(){
    spanPre.innerHTML = "<a href='javascript:pre();'>上一页</a>";
}
function preText(){
    spanPre.innerHTML = "上一页";
}
function nextLink(){
    spanNext.innerHTML = "<a href='javascript:next();'>下一页</a>";
}
function nextText(){
    spanNext.innerHTML = "下一页";
}
function firstLink(){
    spanFirst.innerHTML = "<a href='javascript:first();'>首页</a>";
}
function firstText(){
    spanFirst.innerHTML = "首页";
}
function lastLink(){
    spanLast.innerHTML = "<a href='javascript:last();'>末页</a>";
}
function lastText(){
    spanLast.innerHTML = "末页";
}
//隐藏
function hide(){
    for ( var i = pageSize; i<numberRowsInTable; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
    totalPage.innerHTML = pageCount();
    pageNum.innerHTML = '1';
    nextLink();
    lastLink();
}
hide();